package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.controller.ZnxController;
import com.example.entity.dto.CopChangeDto;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.*;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.CoptcMapper;
import com.example.mapper.CoptdMapper;
import com.example.mapper.CopteMapper;
import com.example.mapper.CoptfMapper;
import com.example.service.CopteService;
import com.example.service.DscmaService;
import com.example.service.IActFlowCustomService;
import com.example.untils.SecurityUtils;
import com.example.untils.StringUtils;
import com.example.untils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-05-30
 */
@Service
public class CopteServiceImpl extends ServiceImpl<CopteMapper, Copte> implements CopteService, IActFlowCustomService {

    @Resource
    private CopteMapper copteMapper;
    @Resource
    private CoptfMapper coptfMapper;

    @Resource
    private CoptcMapper coptcMapper;

    @Resource
    private CoptdMapper coptdMapper;

    @Resource
    private DscmaService dscmaService;

    @Override
    public Result insertCopChange(CopChangeDto changeDto) {

        return Result.success();
    }

    @Override
    public Boolean tc30IsN(String tc001, String tc002, String tc003) {
        LambdaQueryWrapper<Copte> queryWrapper= new LambdaQueryWrapper<Copte>();
        queryWrapper.eq(Copte::getTE001,tc001).eq(Copte::getTE002,tc002).eq(Copte::getTE003,tc003);
        Copte copte = copteMapper.selectOne(queryWrapper);
        if (copte!=null&&copte.getTE044().equals("0")){
            return true;
        }else {
            throw new BaseException(tc001+"-"+tc002+"-"+tc003+"该销售变更单不是未审核销售变更单");
        }
    }

    /**
     * 审批完成后，把变更单中的内容放入销售订单
     * @param TE001
     * @param TE002
     * @param TE003
     */
    @Override
    @DSTransactional
    public void copteInsertCoptc(String TE001, String TE002, String TE003, String assignee) {
        Copte copte11 = new Copte();
        copte11.setTE029("Y");
        copte11.setTE044("3");
        copteMapper.update(copte11, new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, TE001).eq(Copte::getTE002, TE002).eq(Copte::getTE003, TE003));

        Coptf coptf11 = new Coptf();
        coptf11.setTF019("Y");
        coptfMapper.update(coptf11, new LambdaQueryWrapper<Coptf>().eq(Coptf::getTF001, TE001).eq(Coptf::getTF002, TE002).eq(Coptf::getTF003, TE003));

        Dscma dscma = dscmaService.getOne(new LambdaQueryWrapper<Dscma>().eq(Dscma::getMa001, assignee));
        if(dscma == null) {
            throw new CustomException("500", "审核者不存在!");
        }

        Copte copte = copteMapper.selectOne(new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, TE001).eq(Copte::getTE002, TE002).eq(Copte::getTE003, TE003));
        Coptc coptc = new Coptc();
        coptc.setTc004(copte.getTE007());
        coptc.setTc005(copte.getTE008());
        coptc.setTc006(copte.getTE009());
        coptc.setTc007(copte.getTE010());
        coptc.setTc008(copte.getTE011());
        coptc.setTc009(copte.getTE012());
        coptc.setTc010(copte.getTE013());
        coptc.setTc011(copte.getTE014());
        coptc.setTc012(copte.getTE015());
        coptc.setTc013(copte.getTE016());
        coptc.setTc014(copte.getTE017());
        coptc.setREMARK(copte.getTE052());  // 备注
        coptc.setTc016(copte.getTE018());
        coptc.setTc017(copte.getTE019());
        coptc.setTc018(copte.getTE020());
        coptc.setTc019(copte.getTE021());
        coptc.setTc020(copte.getTE022());
        coptc.setTc021(copte.getTE023());
        coptc.setTc022(copte.getTE024());
        coptc.setTc023(copte.getTE025());
        coptc.setTc024(copte.getTE026());
        coptc.setTc025(copte.getTE027());
        coptc.setTc026(copte.getTE028());
//        coptc.setTc027(copte.getTE029());    审核码
        coptc.setTc028(copte.getTE030());

//        coptc.setTc029(copte.getTEI03());
//        coptc.setTc030(copte.getTEI04());
//        coptc.setTc031();  // 数量

        coptc.setTc032(copte.getTE031());
        coptc.setTc033(copte.getTE032());
        coptc.setTc034(copte.getTE033());
        coptc.setTc035(copte.getTE034());
        coptc.setTc036(copte.getTE035());
        coptc.setTc037(copte.getTE036());
        coptc.setTc038(copte.getTE037());
//        coptc.setTc039(copte.getTE038());
        coptc.setTc040(copte.getTE039());
        coptc.setTc041(copte.getTE040());
        coptc.setTc042(copte.getTE041());

        coptc.setTc045(copte.getTE042());
        coptc.setTc047(copte.getTE043());
        coptc.setTc048(copte.getTE044());
        coptc.setTc050(copte.getTE045());

        coptc.setTc052(copte.getTE053());
        coptc.setTc053(copte.getTE054());
        coptc.setTc054(copte.getTE055());
        coptc.setTc055(copte.getTE056());
        coptc.setTc056(copte.getTE141());
        coptc.setTc057(copte.getTE142());

        coptc.setTc073(copte.getTE061());
        coptc.setTc074(copte.getTE062());
        coptc.setTc075(copte.getTE063());

        coptc.setTc077(copte.getTE065());

        coptc.setTc003(copte.getUDF01());  // 新订单日期
        coptc.setTci01(copte.getUDF07());  // 客户合同交期
        coptc.setTci02(copte.getUDF03());  // 客户订单日期
        coptc.setUdf02(copte.getUDF05());  // 新交易条件
        coptc.setUdf06(copte.getUDF09());  // 客户收货地址编号
        coptc.setUdf01(copte.getUDF11());  // 供应链采购单号
        coptc.setUdf04(copte.getTE047());  // 收货客户联系人

        List<Coptf> coptfList = coptfMapper.selectList(new LambdaQueryWrapper<Coptf>().eq(Coptf::getTF001, TE001).eq(Coptf::getTF002, TE002).eq(Coptf::getTF003, TE003));
        for(Coptf coptf : coptfList) {
            Coptd coptd = new Coptd();
            coptd.setTd004(coptf.getTF005());
            coptd.setTd005(coptf.getTF006());
            coptd.setTd006(coptf.getTF007());
            coptd.setTd007(coptf.getTF008());
            coptd.setTd008(coptf.getTF009());

            coptd.setTd010(coptf.getTF010());
            coptd.setTd011(coptf.getTF013());
            coptd.setTd012(coptf.getTF014());
            coptd.setTd013(coptf.getTF015());
            coptd.setTd014(coptf.getTF016());
            coptd.setTd015(coptf.getTF036());

            coptd.setTd020(coptf.getTF033());
            coptd.setTd022(coptf.getTF011());
            coptd.setTd023(coptf.getTF012());
            coptd.setTd024(coptf.getTF020());
            coptd.setTd026(coptf.getTF021());
            coptd.setTd027(coptf.getTF022());
            coptd.setTd029(coptf.getTF023());
            coptd.setTd030(coptf.getTF024());
            coptd.setTd031(coptf.getTF025());
            coptd.setTd032(coptf.getTF026());
            coptd.setTd034(coptf.getTF027());
            coptd.setTd036(coptf.getTF028());
            coptd.setTd037(coptf.getTF029());
            coptd.setTd038(coptf.getTF030());
            coptd.setTd039(coptf.getTF031());

            coptd.setTdi01(coptf.getTFI01());
            coptd.setTdi02(coptf.getTFI02());
            coptd.setTdi03(coptf.getTFI03());
            coptd.setTd061(coptf.getTF042());
            coptd.setTd062(coptf.getTF043());

            coptd.setUdf03(coptf.getUDF06());  // 生管确认日
            coptd.setUdf04(coptf.getUDF02());  // 客户品名
            coptd.setUdf05(coptf.getUDF04());  // 客户品号规格

            coptd.setUdf51(coptf.getUDF51());  // 总金额(六位数)
            coptd.setUdf52(coptf.getUDF53());
            coptd.setUdf53(coptf.getUDF55());

            coptd.setUdf54(coptf.getUDF57());  // 总金额(六位数)
            coptd.setUdf55(coptf.getUDF59());
            coptd.setUdf56(coptf.getUDF61());
            if(coptf.getTF054().equals("1")) {  // 新增
                coptd.setCreateDate(TimeUtils.getNowTime());
                coptd.setCreator(assignee);

                coptd.setUdf11(dscma.getMa002());   // 创建者名称

                coptd.setTd001(TE001);
                coptd.setTd002(TE002);
                coptd.setTd003(coptf.getTF104());  // 序号
                coptdMapper.insert(coptd);
            }else if (coptf.getTF054().equals("2")) {  //  修改
                coptd.setModiDate(TimeUtils.getNowTime());
                coptd.setModifier(assignee);
                coptd.setUdf12(dscma.getMa002());   // 修改者名称
                coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TE001).eq(Coptd::getTd002, TE002).eq(Coptd::getTd003, coptf.getTF104()));
            } else if (coptf.getTF054().equals("3")) {  // 指定结束
                coptd.setModiDate(TimeUtils.getNowTime());
                coptd.setModifier(assignee);
                coptd.setUdf12(dscma.getMa002());  // 修改者名称
                coptd.setTd016(coptf.getTF017());
                coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TE001).eq(Coptd::getTd002, TE002).eq(Coptd::getTd003, coptf.getTF104()));
            }
        }
        Coptc sumNums = coptdMapper.getSumNums(TE001, TE002);
        coptc.setTc029(sumNums.getTc029());  // 税前金额
        coptc.setTc030(sumNums.getTc030());  // 税额
        coptc.setTc031(sumNums.getTc031());   // 数量
        coptc.setTci03(sumNums.getTci03());  // 本币税前金额
        coptc.setTci04(sumNums.getTci04());  // 本币税额
        coptc.setUdf51(sumNums.getUdf51());  // 总金额（六位数的）
        coptc.setUdf52(sumNums.getUdf52());   // 自定义税前金额(6位)
        coptc.setUdf53(sumNums.getUdf53());   // 自定义税额(4位)

        coptc.setModifier(assignee);
        coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>().eq(Coptc::getTc001, TE001).eq(Coptc::getTc002, TE002));
    }

    /**
     * 撤审后，把变更单中的内容放入销售订单
     * @param TE001
     * @param TE002
     * @param TE003 版本号
     */
    @Override
    @DSTransactional
    public void unchecked(String TE001, String TE002, String TE003) {
        Copte copte = copteMapper.selectOne(new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, TE001).eq(Copte::getTE002, TE002).eq(Copte::getTE003, TE003));
        Coptc coptc = new Coptc();
        coptc.setTc004(copte.getTE107());
        coptc.setTc005(copte.getTE108());
        coptc.setTc006(copte.getTE109());
        coptc.setTc007(copte.getTE110());
        coptc.setTc008(copte.getTE111());
        coptc.setTc009(copte.getTE112());
        coptc.setTc010(copte.getTE113());
        coptc.setTc011(copte.getTE114());
        coptc.setTc012(copte.getTE115());
        coptc.setTc013(copte.getTE116());
        coptc.setTc014(copte.getTE117());
        coptc.setREMARK(copte.getTE152());  // 备注
        coptc.setTc016(copte.getTE118());
        coptc.setTc017(copte.getTE119());
        coptc.setTc018(copte.getTE120());
        coptc.setTc019(copte.getTE121());
        coptc.setTc020(copte.getTE122());
        coptc.setTc021(copte.getTE123());
        coptc.setTc022(copte.getTE124());
        coptc.setTc023(copte.getTE125());
        coptc.setTc024(copte.getTE126());
        coptc.setTc025(copte.getTE127());
        coptc.setTc026(copte.getTE128());
//        coptc.setTc027(copte.getTE029());  审核码
//        coptc.setTc028(copte.getTE030());  打印次数

//        coptc.setTc029(copte.getTEI03());
//        coptc.setTc030(copte.getTEI04());
//        coptc.setTc031();  // 数量

        coptc.setTc032(copte.getTE129());
        coptc.setTc033(copte.getTE130());
        coptc.setTc034(copte.getTE131());
        coptc.setTc035(copte.getTE132());
        coptc.setTc036(copte.getTE133());
        coptc.setTc037(copte.getTE134());
        coptc.setTc038(copte.getTE135());
//        coptc.setTc039(copte.getTE038());   单据日期
//        coptc.setTc040(copte.getTE039());   审核者
        coptc.setTc041(copte.getTE136());
        coptc.setTc042(copte.getTE137());

        coptc.setTc045(copte.getTE138());
        coptc.setTc047(copte.getTE139());
//        coptc.setTc048(copte.getTE044());   签核状态码
//        coptc.setTc050(copte.getTE045());   抛转状态

        coptc.setTc052(copte.getTE153());
        coptc.setTc053(copte.getTE154());
        coptc.setTc054(copte.getTE155());
        coptc.setTc055(copte.getTE156());
        coptc.setTc056(copte.getTE143());
        coptc.setTc057(copte.getTE144());

        coptc.setTc073(copte.getTE158());
        coptc.setTc074(copte.getTE159());
        coptc.setTc075(copte.getTE160());

        coptc.setTc077(copte.getTE162());

        coptc.setTc003(copte.getUDF02());  // 新订单日期
        coptc.setTci01(copte.getUDF08());  // 客户合同交期
        coptc.setTci02(copte.getUDF04());  // 客户订单日期
        coptc.setUdf02(copte.getUDF06());  // 新交易条件
        coptc.setUdf06(copte.getUDF10());  // 客户收货地址编号
        coptc.setUdf01(copte.getUDF12());  // 供应链采购单号
        coptc.setUdf04(copte.getTE048());  // 收货客户联系人
        List<Coptf> coptfList = coptfMapper.selectList(new LambdaQueryWrapper<Coptf>().eq(Coptf::getTF001, TE001).eq(Coptf::getTF002, TE002).eq(Coptf::getTF003, TE003));
        for(Coptf coptf : coptfList) {
            Coptd coptd = new Coptd();
            coptd.setTd004(coptf.getTF105());
            coptd.setTd005(coptf.getTF106());
            coptd.setTd006(coptf.getTF107());
            coptd.setTd007(coptf.getTF108());
            coptd.setTd008(coptf.getTF109());

            coptd.setTd010(coptf.getTF110());
            coptd.setTd011(coptf.getTF113());
            coptd.setTd012(coptf.getTF114());
            coptd.setTd013(coptf.getTF115());
            coptd.setTd014(coptf.getTF116());
            coptd.setTd015(coptf.getTF142());

            coptd.setTd020(coptf.getTF139());
            coptd.setTd022(coptf.getTF111());
            coptd.setTd023(coptf.getTF112());
            coptd.setTd024(coptf.getTF120());
            coptd.setTd026(coptf.getTF121());
            coptd.setTd027(coptf.getTF122());
            coptd.setTd029(coptf.getTF125());
            coptd.setTd030(coptf.getTF126());
            coptd.setTd031(coptf.getTF127());
            coptd.setTd032(coptf.getTF128());
            coptd.setTd034(coptf.getTF129());
            coptd.setTd036(coptf.getTF132());
            coptd.setTd037(coptf.getTF133());  // 税率
            coptd.setTd038(coptf.getTF134());
            coptd.setTd039(coptf.getTF135());

            coptd.setTdi01(coptf.getTFI11());
            coptd.setTdi02(coptf.getTFI12());
            coptd.setTdi03(coptf.getTFI13());
            coptd.setTd061(coptf.getTF153());
            coptd.setTd062(coptf.getTF154());

            coptd.setUdf03(coptf.getUDF05());  // 生管确认日
            coptd.setUdf04(coptf.getUDF01());  // 客户品名
            coptd.setUdf05(coptf.getUDF03());  // 客户品号规格

//            coptd.setUdf51(coptf.getTF143());  // 总金额(六位数)
            coptd.setUdf51(coptf.getUDF52());  // 总金额(六位数)
            coptd.setUdf52(coptf.getUDF54());
            coptd.setUdf53(coptf.getUDF56());

            coptd.setUdf54(coptf.getUDF58());  // 总金额(六位数)
            coptd.setUdf55(coptf.getUDF60());
            coptd.setUdf56(coptf.getUDF62());
            if(coptf.getTF054().equals("1")) {  // 新增 反过来就是删除
                coptd.setTd001(TE001);
                coptd.setTd002(TE002);
                coptd.setTd003(coptf.getTF104());  // 序号
                coptdMapper.delete(new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TE001).eq(Coptd::getTd002, TE002).eq(Coptd::getTd003, coptf.getTF104()));
            }else if (coptf.getTF054().equals("2")) {  //  修改
                coptd.setModiDate(TimeUtils.getNowTime());
                coptd.setModifier(SecurityUtils.getLoginUser().getUsername());
                coptd.setUdf12(SecurityUtils.getLoginUser().getName());
                coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TE001).eq(Coptd::getTd002, TE002).eq(Coptd::getTd003, coptf.getTF104()));
            } else if (coptf.getTF054().equals("3")) {  // 指定结束
                coptd.setModiDate(TimeUtils.getNowTime());
                coptd.setModifier(SecurityUtils.getLoginUser().getUsername());
                coptd.setUdf12(SecurityUtils.getLoginUser().getName());
                coptd.setTd016(coptf.getTF117());
                coptdMapper.update(coptd, new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, TE001).eq(Coptd::getTd002, TE002).eq(Coptd::getTd003, coptf.getTF104()));
            }
        }
        Coptc sumNums = coptdMapper.getSumNums(TE001, TE002);
        coptc.setTc029(sumNums.getTc029());  // 税前金额
        coptc.setTc030(sumNums.getTc030());  // 税额
        coptc.setTc031(sumNums.getTc031());   // 数量
        coptc.setTci03(sumNums.getTci03());  // 本币税前金额
        coptc.setTci04(sumNums.getTci04());  // 本币税额
        coptc.setUdf51(sumNums.getUdf51());  // 总金额（六位数的）
        coptc.setUdf52(sumNums.getUdf52());   // 自定义税前金额(6位)
        coptc.setUdf53(sumNums.getUdf53());   // 自定义税额(4位)
        coptcMapper.update(coptc, new LambdaQueryWrapper<Coptc>().eq(Coptc::getTc001, TE001).eq(Coptc::getTc002, TE002));
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","销售变更单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Override
    public void startRunTask(String id) {

    }

    @Autowired
    private ZnxController znxController;

    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("销售变更单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Copte copte = copteMapper.selectOne(new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, strings[0]).eq(Copte::getTE002, strings[1]));
        if(copte == null) {
            throw new CustomException("500", "委外核价单不存在");
        }
        if(state) {  // 审核通过
            copte.setTE044("3");
            copteMapper.update(copte, new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, strings[0]).eq(Copte::getTE002, strings[1]));
            checkmoctm(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的销售变更单单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            copte.setTE044("2");
            copteMapper.update(copte, new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, strings[0]).eq(Copte::getTE002, strings[1]));
        }
    }

    public void checkmoctm(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Copte copte = copteMapper.selectOne(new LambdaQueryWrapper<Copte>().eq(Copte::getTE001, TA001).eq(Copte::getTE002, TA002));
        if(copte == null) {
            throw new CustomException("500", "单据不存在!");
        }
        copte.setTE029("Y");
        copteMapper.update(copte, new LambdaQueryWrapper<Copte>().eq(Copte::getTE001,TA001).eq(Copte::getTE002,TA002));
        Coptf coptf = new Coptf();
        coptf.setTF019("Y");
        coptfMapper.update(coptf, new LambdaQueryWrapper<Coptf>().eq(Coptf::getTF001, TA001).eq(Coptf::getTF002, TA002));
    }

    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }
}
